setwd("~/lab2r")
library("class")
library("foreach")                                                                                #load libraries to perform loops in parallel (accelerate code)
library("doMC")
library("hash")
number.of.processors <- 4                                                                       #set the number of available processors (6 is better)
registerDoMC(number.of.processors)
listWindowsSize = seq(from=20, to=100, by=20)
indexList = seq(1,36,1)
kList = seq(3, 13, 2)

f.measure <- function(target, prediction) {
    classes <- sort(unique(target))
    nData <- length(target)
    fMeasure <- 0

    for(i in classes) {
        n <- sum(target==i)                                                     #number of elements of class i
        tp <- sum((target==i) & (prediction==i))                                #number of true positifs
        fp <- sum((target!=i) & (prediction==i))                                #number of false positifs
        fn <- sum((target==i) & (prediction!=i))                                #number of false negatives
        precision <- tp / (tp + fp)                                             #precision
        recall <- tp / (tp + fn)                                                #recall
        temp <- 2 * (n/nData) * ((precision * recall) / (precision + recall))   #F1-Score formula (weighted)
        if (!is.nan(temp)) {
            fMeasure <- fMeasure + temp
        }
    }
    fMeasure
}
#able to change k, index (sequential or random), windows size, 
labels20 <- read.table("label_w20_o10.tab", sep="\t", header=FALSE)[,1] - 1                           #read the labels
dataset20 <- read.table("meanSD_w20_o10.tab", sep="\t", header=FALSE, colClasses=rep("numeric",12))   #read the features
labels40 <- read.table("label_w40_o20.tab", sep="\t", header=FALSE)[,1] - 1                           #read the labels
dataset40 <- read.table("meanSD_w40_o20.tab", sep="\t", header=FALSE, colClasses=rep("numeric",12))   #read the features
labels60 <- read.table("label_w60_o30.tab", sep="\t", header=FALSE)[,1] - 1                           #read the labels
dataset60 <- read.table("meanSD_w60_o30.tab", sep="\t", header=FALSE, colClasses=rep("numeric",12))   #read the features
labels80 <- read.table("label_w80_o40.tab", sep="\t", header=FALSE)[,1] - 1                           #read the labels
dataset80 <- read.table("meanSD_w80_o40.tab", sep="\t", header=FALSE, colClasses=rep("numeric",12))   #read the features
labels100 <- read.table("label_w100_o50.tab", sep="\t", header=FALSE)[,1] - 1                           #read the labels
dataset100 <- read.table("meanSD_w100_o50.tab", sep="\t", header=FALSE, colClasses=rep("numeric",12))   #read the features

listLabel <- c(list(labels20), list(labels40), list(labels60), list(labels80), list(labels100))
listData <- c(list(dataset20), list(dataset40), list(dataset60), list(dataset80), list(dataset100))

index <- seq(1,36,1)                                                                              
fscoreResult <- foreach (k = kList) %dopar% {                                   
  
  tmp =hash()
  for(indexWindowsSize in 1:length(listLabel)) {
    dataset <- listData[[indexWindowsSize]]
    labels <- listLabel[[indexWindowsSize]]
    test <- knn.cv(dataset[,index], labels, k=k)                                                       
    score = f.measure(labels[labels!=0], test[labels!=0])
    toto =paste("windows size", indexWindowsSize*20, "k", k)
    tmp[[toto]] = score   
  }
  tmp
}
for (c in fscoreResult){
  print(c)
}